home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c-part1 / 4193 < prev    next >
Encoding:
Internet Message Format  |  1996-08-05  |  3.1 KB

  1. Path: news.iag.net!news
  2. From: jatmon@iag.net (John R Buchan)
  3. Newsgroups: comp.lang.c
  4. Subject: Re: simple code, argc, argv, strcmp()
  5. Date: 2 Feb 1996 17:55:56 GMT
  6. Organization: Internet Access Group, Orlando, Florida
  7. Message-ID: <4etj7c$bma@news.iag.net>
  8. References: <11f7cc$17261a.3b3@daprez>
  9. NNTP-Posting-Host: pm2-orl22.iag.net
  10. Keywords: strcmp encode decode argc argv
  11. X-Newsreader: WinVN 0.99.7
  12.  
  13. In article <11f7cc$17261a.3b3@daprez>, otisg@panther.middlebury.edu says...
  14. >
  15. >Hi, a C question...
  16. >this one has been bothering ALL afternoon !
  17. >
  18. >This snippet of code is supposed to call appropriate function based on what
  19. >command line arguments are supplied.
  20. >
  21. >usage: <program> -e [SourceFile] RemoteFile
  22. >        or
  23. >       <program> -d [InFile]
  24. >
  25. >if the person doesn't use this right the program is supposed to call Usage()
  26. >which is not here, but it's in my code, and if the person calls the program
  27. >the correct way one of the two actions should be taken (encode or decode).
  28. >
  29. >Problem - I am doing something wrong and I almost always end up calling
  30. >Usage() even if I use the program correctly.
  31. >
  32. >#include <stdio.h>
  33. >#include <stdlib.h>
  34. >#include <string.h>
  35. >
  36. >int main (int argc, char **argv) {
  37. >
  38. >  void Usage   (void);
  39. >  void Encode (int, char **);
  40. >  void Decode (int, char **);
  41. >
  42. >  if (!strcmp(argv[1],"-d") || !strcmp(argv[1],"-e")) {
  43. >    Usage();
  44. >  }
  45. <snip>
  46.  
  47. I assume you mean this to call Usage if argv[1] is not "-e" and not "-d"?
  48. Change the || to &&.  Of course, if no argument was passed, who knows what
  49. will happen here.  You should always test argc first to be certain that 
  50. the argv element exists.
  51.  
  52. Why are you allowing the code to continue to execute once it has been 
  53. determined that the usage is incorrect?  Try either:
  54.  
  55.    1. follow each call to Usage with exit() (or have Usage call exit).
  56.    2. Use if/else to prevent execution of later statements.
  57.  
  58. You could make this code much easier to read and troubleshoot by grouping 
  59. the if conditions a little or using switch.  Something like:
  60.  
  61.    if( (argc >= 3) && argv[1][0] == '-')
  62.       if( argv[1][1] == 'e')              /* might test == 'E' as well */
  63.          if( argc == 4)                   /* might use >= instead */
  64.             Encode( argc, argv);
  65.          else
  66.             Usage();
  67.       else
  68.          if( argv[1][1] == 'd')
  69.             Decode( argc, argv);          /* already tested >= 3 */
  70.          else 
  71.             Usage();
  72.    else
  73.       Usage();      
  74.  
  75. or
  76.  
  77.    if( (argc >= 3) && argv[1][0] == '-')
  78.       switch( argv[1][1])
  79.          {
  80.          case 'e':
  81.          case 'E':
  82.             if( argc == 4)
  83.                Encode( argc, argv);
  84.             else
  85.                Usage();
  86.             break;
  87.  
  88.          case 'd':
  89.          case 'D':
  90.             Decode( argc, argv);
  91.             break;
  92.  
  93.          default:
  94.             Usage();
  95.          }
  96.       else
  97.          Usage();
  98.  
  99. Note: The program should probably return EXIT_FAILURE, when it fails (eg
  100. incorrect usage).  Probably the simplest way to handle this is to have
  101. Usage call exit(EXIT_FAILURE).
  102.  
  103. -- 
  104. John R Buchan           -:|:-     Looking for that elusive FAQ?  ftp to:
  105. jatmon@mail.iag.net     -:|:-     rtfm.mit.edu /pub/usenet-by-group/....
  106.  
  107.